import {request} from "@/utils/request";
import type {
    CollectionItem,
    FollowPreItem,
    User,
    UserAnimalItem,
    UserDetail,
    UserHomepageData,
    UserMsgDetail
} from "@/types/user";
import type {FeedbackType} from "@/enum/feedbackType";
import type {PageResult} from "@/types/PageResult";
import type {AnimalPreItem} from "@/types/animals";

/**
 *  用户登录
 * @param data 用户名和密码
 * @param code
 * @param codeId // 如果codeId与后端记录不一致将直接返回错误
 */
export const userLoginApi = (data: { username: string, password: string }, code: string, codeId: number) => {
    return request<User>({
        method: "POST",
        url: `/user/login?code=${code}&&codeId=${codeId}`,
        data
    })
}

/**
 *  获取用户详细信息
 */
export const getUserDetailApi = () => {
    return request<UserDetail>({
        method: "GET",
        url: '/user/detail',
    })
}

/**
 *  更新用户信息
 * @param data 用户信息
 */
export const updateUserInfoApi = (data: Omit<User, 'token' | 'userID'>) => {
    return request({
        method: "POST",
        url: '/user/update',
        data
    })
}
/**
 *  检查用户名是否可用
 * @param username 用户名
 */
export const checkUserNameApi = (username: string) => {
    return request({
        method: "POST",
        url: `/user/check/${username}`,
    })
}

/**
 * 获取用户收藏信息
 */
export const getUserFavApi = (page: number, pageSize: number, type: 0 | 1) => {
    return request<PageResult<CollectionItem[]>>({
        method: 'GET',
        url: '/user/fav?page=' + page + '&pageSize=' + pageSize + '&type=' + type,
    })
}

/**
 *  移除用户收藏信息
 * @param favIds
 */
export const deleteUserFavApi = (favIds: string[]) => {
    return request({
        method: 'POST',
        url: '/user/fav/del',
        data: {
            favIds
        }
    })
}

/**
 *  用户反馈
 * @param type
 * @param content
 */
export const feedbackApi = (type: FeedbackType, content: string) => {
    return request({
        method: 'POST',
        url: '/user/feedback?type=' + type + '&content=' + content
    })
}

/**
 * 获取用户上传数据
 * @param page
 * @param pageSize
 * @param type (可选上传数据当前处于的状态)0——待审核；1——审核通过；2——审核未通过;3——手动取消的上传记录
 */
export const getUserUploadDataApi = (page: number, pageSize: number, type?: 0 | 1 | 2 | 3) => {
    return request<PageResult<UserAnimalItem[]>>({
        method: 'GET',
        url: '/user/upload?page=' + page + '&pageSize=' + pageSize + (type !== undefined ? '&type=' + type : '')
    })
}

/**
 * 用户取消上传
 * @param id 该条记录id
 * @param auditId 审核id
 */
export const cancelUserUploadApi = (id: number, auditId: number) => {
    return request({
        method: 'POST',
        url: '/user/upload/cancel?id=' + id + '&auditId=' + auditId,
    })
}

/**
 *  用户删除上传记录
 * @param id
 * @param auditId
 */
export const delUserUploadApi = (id: number, auditId: number) => {
    return request({
        method: 'POST',
        url: '/user/upload/del?id=' + id + '&auditId=' + auditId,
    })
}
/**
 *  用户重新（修改）上传记录
 * @param uploadAnimal
 */
export const reUserUploadApi = (uploadAnimal: UserAnimalItem) => {
    return request({
        method: 'POST',
        url: '/user/upload/re',
        data: uploadAnimal
    })
}

/**
 * 用户获取详细消息
 * @param page
 * @param pageSize
 * @param type 消息类型 0-点赞，1-回复，2-系统
 */
export const getUserMsgDetailApi = (page: number, pageSize: number, type: 0 | 1 | 2) => {
    return request<PageResult<UserMsgDetail[]>>({
        method: 'GET',
        url: '/user/msg/detail?page=' + page + '&pageSize=' + pageSize + '&type=' + type,
    })
}

/**
 * 获取用户主页数据
 */
export const getUserHomeDataApi = (userId: number, type: 0 | 1) => {
    return request<UserHomepageData>({
        method: 'GET',
        url: '/user/home/data?userId=' + userId + '&type=' + type,
    })
}
/**
 * 获取用户主页上传数据
 * @param page
 * @param pageSize
 * @param userId
 * @param type
 * @param isPic
 */
export const getUserHomeUploadApi = (page: number, pageSize: number, userId: number, type: 0 | 1, isPic: 0 | 1) => {
    return request<PageResult<AnimalPreItem[]>>({
        method: 'GET',
        url: '/user/home/upload?page=' + page + '&pageSize=' + pageSize + '&userId=' + userId + '&type=' + type+'&isPic='+isPic,
    })
}

/**
 * 处理用户关注等事件
 * @param userId 用户id（非登录用户）
 * @param type 当前两个用户之间的关系
 */
export const handleUserFollowApi = (userId: number, type: 0 | 1 | 2 | 3) => {
    return request({
        method: 'POST',
        url: '/user/home/follow?userId=' + userId + '&type=' + type,
    })
}

/**
 *  获取用户关注列表
 * @param page
 * @param pageSize
 * @param userId 被获取用户的id
 * @param type 0——自己获取，别人获取
 */
export const getFollowListApi = (page: number, pageSize: number, userId: number, type: 0 | 1) => {
    return request<PageResult<FollowPreItem[]>>({
        method: 'GET',
        url: '/user/follow/list?page=' + page + '&pageSize=' + pageSize + '&userId=' + userId + '&type=' + type,
    })
}
/**
 *  获取用户粉丝列表
 * @param page
 * @param pageSize
 * @param userId 被获取用户的id
 * @param type 0——自己获取，别人获取
 */
export const getFansListApi = (page: number, pageSize: number, userId: number, type: 0 | 1) => {
    return request<PageResult<FollowPreItem[]>>({
        method: 'GET',
        url: '/user/fans/list?page=' + page + '&pageSize=' + pageSize + '&userId=' + userId + '&type=' + type,
    })
}

/**
 * 获取用户本周剩余上传次数
 */
export const getUserUploadCountApi = () => {
    return request<number>({
        method: 'GET',
        url: '/user/upload/count'
    })
}
/**
 * 获取用户本周剩余上传次数
 */
export const getUserAvatarCountApi = () => {
    return request<number>({
        method: 'GET',
        url: '/user/avatar/count'
    })
}

/**
 *  获取用户列表（用户搜索其他用户的时候使用）
 * @param keyword 用户名或者用户id
 * @param page
 * @param pageSize
 */
export const getUserListByKeywordApi = (keyword: string | number, page: number, pageSize: number) => {
    return request<PageResult<User[]>>({
        method: 'GET',
        url: '/user/list?keyword=' + keyword + '&page=' + page + '&pageSize=' + pageSize,
    })
}

// 用户当天进入软件触发
export const userLogin = () => {
    return request<User>({
        method: 'GET',
        url: '/user/dailyLogin'
    })
}

// 确认用户账号信息
export const checkUserStatusApi = () => {
    return request<0 | 1>({
        method: 'GET',
        url: '/user/check/status'
    })
}